package com.xuecheng.content.service.jobhandler;

import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.service.ICoursePublishService;
import com.xuecheng.message.sdk.constant.MqMessageTypeConstant;
import com.xuecheng.message.sdk.service.IMqMessageService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: 长时间下架的课程加到消息表中
 * @date 2025/2/19 9:42
 */
@Component
@RequiredArgsConstructor
public class OfflineToMessageTask {

    /**
     * 课程发布mapper
     */
    private final ICoursePublishService coursePublishService;

    /**
     * 消息服务
     */
    private final IMqMessageService mqMessageService;

    /**
     * 已加入消息表
     */
    private static final String AlreadyAdded = "已加入消息表";

    /**
     * 添加下架消息
     */
    @XxlJob("OfflineToMessageJobHandler")
    public void offlineToMessage(){
        List<CoursePublish> offlineCourses = coursePublishService.selectLongTimeOffline();
        offlineCourses.forEach(coursePublish -> {
            if (!AlreadyAdded.equals(coursePublish.getRemark())){
                mqMessageService.addMessage(MqMessageTypeConstant.MESSAGE_TYPE_COURSE_OFFLINE, String.valueOf(coursePublish.getId()), null, null);
                coursePublishService.lambdaUpdate()
                        .set(CoursePublish::getRemark, AlreadyAdded)
                        .eq(CoursePublish::getId, coursePublish.getId())
                        .update();
            }
        });
    }
}
